package com.wx.dao.impl;

import com.wx.dao.DoctorDao;
import com.wx.domain.Doctor;
import com.wx.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * @author 27250
 */
public class DoctorDaoImpl implements DoctorDao {
    public JdbcTemplate jdbcTemplate= new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public Doctor findByUsername(String username) {
        Doctor doctor =null;
        try {
            doctor = jdbcTemplate.query("select * from doctor where username = ?", new BeanPropertyRowMapper<Doctor>(Doctor.class), username).get(0);
        }catch (Exception e){
            return null;
        }
        return doctor;
    }

    @Override
    public List<String> getDepartment() {
        return jdbcTemplate.queryForList("select DISTINCT department from doctor",String.class);
    }

    @Override
    public List<Doctor> getDoctors(String department, int pageIndex, int pageSize) {
        return jdbcTemplate.query("select * from doctor where department = ? order by doctorId limit ?,? ",
                new BeanPropertyRowMapper<>(Doctor.class),
                department,pageIndex,pageSize);
    }

    @Override
    public int getCountByDep(String department) {
        return jdbcTemplate.queryForObject("select count(*) from doctor where department = ?",Integer.class,department);
    }

    @Override
    public void modifyPassword(String username, String password) {
        jdbcTemplate.update("update doctor set password = ? where username = ?",password,username);
    }
}
